package com.bluefish.bltuan.action.user;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.action.OpenAction;
import com.bluefish.bltuan.module.user.User;
import com.bluefish.bltuan.util.StringUtils;

public class ActiveAction extends OpenAction {

    /**
     * 
     */
    private static final long serialVersionUID = -4404843356054940405L;

    private String            email;

    private String            code;

    public void setEmail(String email) {
        this.email = email;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public HttpHeaders index() {
        if (email != null && code != null) {
            User user = this.userService.getByEmailAndActiveCode(email, code);
            if (user != null) {
                if (user.isActivated()) {
                    setMessage(StringUtils.ALREADY_ACTIVED);
                } else {
                    user.setActivated(true);
                    userService.update(user);
                    setMessage(StringUtils.ACTIVED_SUCCESS);
                }
            } else {
                setMessage(StringUtils.USER_NOT_EXIST);
            }
        } else {
            setMessage(StringUtils.ILLEGAL_LINK);
        }
        return new DefaultHttpHeaders(SUCCESS).disableCaching();
    }
}
